from django.http import JsonResponse, Http404
from django.views.generic import CreateView, ListView, UpdateView

from custom import SandboxCreateView, SandboxUpdateView, BreadcrumbMixin
from .mixin import LoginRequiredMixin
from .models import Menu


class MenuCreateView(SandboxCreateView):
    """创建菜单"""
    model = Menu
    fields = '__all__'

    # success_url = '/system/rbac/menu/create'  # 成功跳转的路径
    # extra_context = dict(menu_all=Menu.objects.all())

    def get_context_data(self, **kwargs):
        kwargs['menu_all'] = Menu.objects.all()
        return super().get_context_data(**kwargs)


class MenuListView(LoginRequiredMixin, BreadcrumbMixin, ListView):
    """菜单列表页"""
    model = Menu
    context_object_name = "menu_all"


class MenuUpdateView(SandboxUpdateView):
    model = Menu  # Menu.objects.all()
    fields = '__all__'
    template_name_suffix = '_update'  # 模板名称后缀名，即 html 名称必须是 xx_update.html

    # success_url = '/system/rbac/menu/'
    # extra_context = dict(menu_all=Menu.objects.all())

    def get_context_data(self, **kwargs):
        kwargs['menu_all'] = Menu.objects.all()
        return super().get_context_data(**kwargs)

